<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:SettleAccountInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:SettleAccountInfo:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="licenseInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="开通刷卡支付权限" align="center" prop="openCard">
        <template slot-scope="scope">
          <el-switch :disabled="true"
            v-model="scope.row.openCard"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="开通快捷支付权限" align="center" prop="openQuick">
        <template slot-scope="scope">
          <el-switch :disabled="true"
            v-model="scope.row.openQuick"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="开通微信支付宝支付权限" align="center" prop="openScan">
        <template slot-scope="scope">
          <el-switch :disabled="true"
            v-model="scope.row.openScan"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="开通银联扫码支付权限" align="center" prop="openUnion">
        <template slot-scope="scope">
          <el-switch :disabled="true"
            v-model="scope.row.openUnion"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:SettleAccountInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改结算信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="开通刷卡支付权限" prop="openCard">
              <el-switch
                v-model="form.openCard"
                active-color="#13ce66"
                inactive-color="#f0f0f0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开通快捷支付权限" prop="openQuick">
              <el-switch
                v-model="form.openQuick"
                active-color="#13ce66"
                inactive-color="#f0f0f0"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开通微信支付宝支付权限" prop="openScan">
              <el-switch
                v-model="form.openScan"
                active-color="#13ce66"
                inactive-color="#f0f0f0"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开通银联扫码支付权限" prop="openUnion">
              <el-switch
                v-model="form.openUnion"
                active-color="#13ce66"
                inactive-color="#f0f0f0"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="费率类型" prop="feeType">
          <el-select v-model="feeType" @change="clearPro" placeholder="请选择费率类型">
            <el-option
              v-for="dict in dict.type.fee_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="t0费率详情列表">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleChildAdd('t0')"
            v-hasPermi="['system:SettleAccountInfo:add']"
          >新增</el-button>
          <el-table :data="t0FeeDetailList">
            <el-table-column label="费率" align="center" prop="fee" />
            <el-table-column label="借记卡费率" align="center" prop="feeDebit" />
            <el-table-column label="贷记卡费率" align="center" prop="feeCredit" />
            <el-table-column label="借记卡封顶费用" align="center" prop="feeDebitMax" />
            <el-table-column label="固定提现费" align="center" prop="feeFixed" />
            <el-table-column label="借记卡固定提现费" align="center" prop="feeDebitFixed" />
            <el-table-column label="贷记卡固定提现费" align="center" prop="feeCreditFixed" />
            <el-table-column label="比率提现费" align="center" prop="feeFixedRate"/>
            <el-table-column label="借记卡比率提现费" align="center" prop="feeDebitFixedRate" />
            <el-table-column label="贷记卡比率提现费" align="center" prop="feeCreditFixedRate" />
            <el-table-column label="金额区间" align="center" prop="transAmount" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleChlidDelete(scope.row,'t0')"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="t1费率详情列表">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleChildAdd('t1')"
          v-hasPermi="['system:SettleAccountInfo:add']"
        >新增</el-button>
        <el-table :data="t1FeeDetailList">
          <el-table-column label="费率" align="center" prop="fee" />
          <el-table-column label="借记卡费率" align="center" prop="feeDebit" />
          <el-table-column label="贷记卡费率" align="center" prop="feeCredit" />
          <el-table-column label="借记卡封顶费用" align="center" prop="feeDebitMax" />
          <el-table-column label="固定提现费" align="center" prop="feeFixed" />
          <el-table-column label="借记卡固定提现费" align="center" prop="feeDebitFixed" />
          <el-table-column label="贷记卡固定提现费" align="center" prop="feeCreditFixed" />
          <el-table-column label="比率提现费" align="center" prop="feeFixedRate"/>
          <el-table-column label="借记卡比率提现费" align="center" prop="feeDebitFixedRate" />
          <el-table-column label="贷记卡比率提现费" align="center" prop="feeCreditFixedRate" />
          <el-table-column label="金额区间" align="center" prop="transAmount" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleChlidDelete(scope.row,'t1')"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
        <el-form-item label="暂存结果">
          <el-table :data="feeTypeList">
            <el-table-column label="费率类型" align="center" prop="feeType">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.fee_type" :value="scope.row.feeType"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="goclick(scope.row)"
                >详情</el-button>
                <el-button
                  size="mini"
                  type="text"
                  @click="handleChlidDelete(scope.row,'feeType')"
                >删除</el-button>

              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" @click="tempSaveData">保 存</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加子属性" :visible.sync="childOpen" width="500px" append-to-body>
      <el-form ref="tempForm" :model="tempForm" label-width="80px">
        <el-form-item label="费率" prop="fee">
          <el-input v-model="tempForm.fee" />
        </el-form-item>
        <el-form-item label="借记卡费率" prop="feeDebit">
          <el-input v-model="tempForm.feeDebit" />
        </el-form-item>
        <el-form-item label="贷记卡费率" prop="feeCredit">
          <el-input v-model="tempForm.feeCredit" />
        </el-form-item>
        <el-form-item label="借记卡封顶费用" prop="feeDebitMax">
          <el-input v-model="tempForm.feeDebitMax" />
        </el-form-item>
        <el-form-item label="固定提现费" prop="feeFixed">
          <el-input v-model="tempForm.feeFixed"  />
        </el-form-item>
        <el-form-item label="借记卡固定提现费" prop="feeDebitFixed">
          <el-input v-model="tempForm.feeDebitFixed" />
        </el-form-item>
        <el-form-item label="贷记卡固定提现费" prop="feeCreditFixed">
          <el-input v-model="tempForm.feeCreditFixed" />
        </el-form-item>
        <el-form-item label="比率提现费" prop="feeFixedRate">
          <el-input v-model="tempForm.feeFixedRate" />
        </el-form-item>
        <el-form-item label="借记卡比率提现费" prop="feeDebitFixedRate">
          <el-input v-model="tempForm.feeDebitFixedRate" />
        </el-form-item>
        <el-form-item label="贷记卡比率提现费" prop="feeCreditFixedRate">
          <el-input v-model="tempForm.feeCreditFixedRate" />
        </el-form-item>
        <el-form-item label="金额区间" prop="transAmount">
          <el-input v-model="tempForm.transAmount" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="insertList">确 定</el-button>
        <el-button @click="cancelList">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listMerchantFee, getMerchantFee, addMerchantFee, updateMerchantFee, delMerchantFee } from "@/api/posp/merchantFee";

  export default {
    name: "merchantFeeUpdate",
    dicts: ['fee_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 法人信息表格数据
        licenseInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          businessName: null
        },
        // 表单参数
        form:{},
        tempForm:{},
        // 表单校验
        rules: {
        },
        feeType:'',
        t0FeeDetailList:[],
        t1FeeDetailList:[],
        childOpen:false,
        resMapData:new Map(),
        feeTypeList:[],
        feeTypeFlag:''
      };
    },
    created() {
      this.getList();
    },
    methods: {
      clearPro(){
        this.t0FeeDetailList = [];
        this.t1FeeDetailList = [];
      },
      //清空临时表单
      resetTemp() {
        this.tempForm = {
          fee:null,
          feeDebit:null,
          feeCredit:null,
          feeDebitMax:null,
          feeFixed:null,
          feeDebitFixed:null,
          feeCreditFixed:null,
          feeFixedRate:null,
          feeDebitFixedRate:null,
          feeCreditFixedRate:null,
          transAmount:null
        };
        this.feeTypeFlag = ''
      },
      //费率类型不为空打开子弹框
      handleChildAdd(flag){
        if(this.feeType){
          this.resetTemp();
          this.feeTypeFlag = flag;
          this.childOpen = true;
        }else{
          this.$alert("请先选择费率类型", '系统提示', {
            type: 'warning'
          })
        }
      },
      insertList(){
        if(this.feeTypeFlag == 't0'){
          this.t0FeeDetailList.push(this.tempForm);
        }else if(this.feeTypeFlag == 't1'){
          this.t1FeeDetailList.push(this.tempForm);
        }
        this.childOpen = false;
      },
      tempSaveData(){
        if(this.feeType){
          this.resMapData.delete(this.feeType);
          let map = new Map();
          if(this.feeType){
            map.set("feeType",this.feeType);
          }
          if(this.t0FeeDetailList.length > 0){
            map.set("t0FeeDetailList",this.t0FeeDetailList);
          }
          if(this.t1FeeDetailList.length > 0){
            map.set("t1FeeDetailList",this.t1FeeDetailList);
          }
          this.resMapData.set(this.feeType,map);
          this.feeType = '';
          this.feeTypeList = [];
          this.t0FeeDetailList = [];
          this.t1FeeDetailList = [];
          for (let key of this.resMapData.keys()) {
            let feeObj = {"feeType":key};
            this.feeTypeList.push(feeObj);
          }
        }
      },
      goclick(row) {
        let obj = this.resMapData .get(row.feeType);
        this.feeType = obj.get('feeType');
        this.t0FeeDetailList = obj.get('t0FeeDetailList');
        this.t1FeeDetailList = obj.get('t1FeeDetailList');
      },
      handleChlidDelete(row,flag){
        var list;
        if(flag == 't0'){
          list = this.t0FeeDetailList
        }else if(flag == 't1'){
          list = this.t1FeeDetailList
        }else if(flag == 'feeType'){
          list = this.feeTypeList
        }
        list = list.filter((ele) => {
          var f = false;
          for(const key in row){
            if(ele[key] != row[key]){
              f = true;
              break;
            }
          }
          return f;
        })
        if(flag == 't0'){
          this.t0FeeDetailList = list
        }else if(flag == 't1'){
          this.t1FeeDetailList = list
        }else if(flag == 'feeType'){
          this.feeTypeList = list
          if(this.resMapData.has(row.feeType)){
            this.resMapData.delete(row.feeType);
            this.feeType = '';
            this.t0FeeDetailList = [];
            this.t1FeeDetailList = [];
          }
        }
      },
      /** 查询结算信息列表 */
      getList() {
        this.loading = true;
        listMerchantFee(this.queryParams).then(response => {
          this.licenseInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      cancelList() {
        this.childOpen = false;
      },
      // 表单重置
      reset() {
        this.form = {
          openCard:false,
          openQuick:false,
          openScan:false,
          openUnion:false,
          feeList:[]
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.feeTypeList = [];
        this.t0FeeDetailList = [];
        this.t1FeeDetailList = [];
        this.resMapData = new Map();
        this.open = true;
        this.title = "添加费率信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.feeTypeList = [];
        this.t0FeeDetailList = [];
        this.t1FeeDetailList = [];
        this.resMapData = new Map();
        const id = row.id || this.ids
        getMerchantFee(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改费率信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        if(this.resMapData.size > 0){
          let arr  = [];
          this.resMapData.forEach(function(value, key) {
            let obj = {
              feeType : value.get("feeType"),
              t0FeeDetailList : value.get("t0FeeDetailList"),
              t1FeeDetailList : value.get("t1FeeDetailList")
            }
            arr.push(obj);
          })
          this.form.feeList = arr;
          this.form.merchantId = this.$route.query.merchantId;
          console.log(this.form)
          if (this.form.id != null && this.form.id != '') {
            updateMerchantFee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            addMerchantFee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            })
          }
        }else {
          this.$alert("请先保存数据", '系统提示', {
            type: 'warning'
          })
        }
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除数据项？').then(function() {
          return delMerchantFee(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
